<template>
  <div>
    <el-dialog
      :title="obj.isAdd ? '规格添加' : '规格编辑'"
      :visible.sync="obj.isTrue"
      width="40%"
      @close="cancel()"
    >
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="规格名称">
          <el-input v-model="form.specsname"></el-input>
        </el-form-item>
        <el-form-item 
          label="规格属性"
          v-for="(item, index) in attrArr"
          :key="index"
        >
          <div class="smBox">
            <el-input v-model="item.val"></el-input>
            <el-button type="primary" v-show="index == 0" @click="attrAdd()"
              >新增规格属性</el-button>
            <el-button type="info" v-show="index != 0" @click="attrPre(index)"
              >删除</el-button
            >
          </div>
        </el-form-item>
        <el-form-item label="状态">
          <el-switch
            v-model="form.status"
            :active-value="1"
            :inactive-value="2"
          ></el-switch>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancel()">取 消</el-button>
        <el-button type="primary" @click="add()" v-if="obj.isAdd">添 加</el-button>
        <el-button type="primary" @click="enterEdit()" v-else>编 辑</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { mapGetters, mapActions } from "vuex";
import { specsAdd, specsInfo, specsEdit } from "../../../request/api";
import { successAlert, warningAlert } from "../../../utils/alert";
export default {
  props: ["obj"],
  components: {},
  data() {
    return {
      attrArr: [{ val: "" }, { val: "" }],
      form: {
        specsname: "",
        attrs: "",
        status: 1,
      },
    };
  },
  computed: {
    ...mapGetters({
      specsList:"specs/list",
      specsCount:"specs/count"
    }),
  },
  methods: {
    ...mapActions({
        asyncSpecsList:"specs/asyncList",
      
        asyncSpecsCount:"specs/asyncCount",
      
    }),
    attrAdd() {
      this.attrArr.push({ val: "" });
    },
    attrPre(index) {
      this.attrArr.splice(index, 1);
    },
    empty() {
      this.form = {
      specsname: "",
        attrs: "",
        status: 1,
      };
      this.attrArr=[{ val: "" }, { val: "" }]
    },
    cancel() {
      this.empty();
      
      this.obj.isTrue = false;
    },
    add() {
      console.log(this.form);
     let arr = this.attrArr.map(item=>item.val)
        this.form.attrs = JSON.stringify(arr)
      specsAdd(this.form).then((res) => {
        if (res.data.code == 200) {
          // 添加成功
          successAlert(res.data.msg);
          // 清空form并关闭对话框
          this.cancel();
          // 刷新menu的列表
          this.asyncSpecsList();

          this.asyncSpecsCount()
        } else {
          // 添加失败
          warningAlert(res.data.msg);
        }
      });
    },
    getOne(id) {
     specsInfo({ id: id }).then((res) => {
        // 给form赋值
        this.form = res.data.list[0];

      this.form.attrs = JSON.parse(this.form.attrs)
       this.attrArr= this.form.attrs.map(item=>{
          return{
            val:item
          }
        })
        // 给form添加id字段，因为后面确定编辑的时候需要用到
        this.form.id = id;
      });
    },
    enterEdit() {
      this.form.attrs =JSON.stringify(this.attrArr.map(item=>item.val))
       specsEdit(this.form).then((res) => {
      if (res.data.code == 200) {
          // 成功提示窗
          successAlert(res.data.msg);
          // 关闭当前遮罩层并清空数据
          this.cancel();
          // 重新请求列表数据
          this.asyncSpecsList();
        } else {
          // 失败提示窗
          warningAlert(res.data.msg);
        }
      });
    },
  },
  mounted() {},
};
</script>
<style scoped>
.smBox {
  display: flex;
}
.smBox input {
  flex: 1;
}
.smBox button {
  margin-left: 0;
}
</style>